const bodyParser = require('body-parser'),
	fs = require('fs'),
	PORT = 8000;

// const jsonParser = bodyParser.json()

/*-CREATE SERVER-*/
const express = require('express'),
	app = express();
app.listen(PORT, () => {
	console.log(`当前服务起于${PORT}端口!`);
});

/*-MIDDLE WARE-*/
app.use(bodyParser.urlencoded({
	extended: false
}));

app.use(bodyParser.json())

/*-QUERY DATA-*/
app.post('/list', async (req, res) => {
	if (req.body.type === 'topic') {
		res.send(fs.readFileSync('./data.json', 'utf-8'));
	} else {
		res.send(fs.readFileSync('./data2.json', 'utf-8'));
	}
});

app.post('/category', async (req, res) => {
	console.log('请求', req.body)
	// res.send(`...${req} === ${res}`)
	const id = req.body.id
	switch (id) {
		case 66:
			res.send(fs.readFileSync('./category.json', 'utf-8'))
			break;
		default:
			res.send(fs.readFileSync('./category1.json', 'utf-8'))
			break;
	}
	// res.send(fs.readFileSync('./category.json', 'utf-8'))
})

app.use(express.static('./dist'));
app.use((_, res) => {
	res.status(404);
	res.send('NOT FOUND!');
});